import request from "../request";
import { ElMessage } from 'element-plus'
import { roleIdtoString } from "@/utils/transform";

/**
 * 获取角色列表接口
 * @returns Object
 */
export const getRoleList = async (searchForm) => {
    const data = await request.get('/auth/role/list', {
        params: searchForm
    });
    return data.data;
}

/**
 * 获取所有角色接口
 * @returns Object
 */
export const getAllRole = async () => {
    const data = await request.get('/auth/role/all');
    return roleIdtoString(data.data) || [];
}

/**
 * 获取角色信息接口
 * @returns Object
 */
export const getRoleInfo = async (id) => {
    const data = await request.get(`/auth/role/one/${id}`);
    return data.data;
}

/**
 * 获取角色菜单权限id接口
 * @returns Object
 */
export const getMenuIds = async (id) => {
    const data = await request.get(`/auth/role/menuIds/${id}`);
    return data.data;
}

/**
 * 获取菜单列表接口
 * @returns Object
 */
export const getMenu = async () => {
    const data = await request.get('/auth/role/menu');
    return data.data;
}

/**
 * 新增角色
 * @returns null
 */
export const addRole = async (commitForm) => {
    await request.post('/auth/role/add', commitForm);
    ElMessage.success('新增权限成功');
}

/**
 * 修改角色
 * @returns null
 */
export const updateRole = async (commitForm) => {
    await request.put('/auth/role/update', commitForm);
    ElMessage.success('修改权限成功');
}

/**
 * 修改角色
 * @returns null
 */
export const deleteRole = async (ids) => {
    const data = await request.delete(`/auth/role/delete/${ids}`);
    if (data.code !== 100) ElMessage.success('删除权限成功');
}

/**
 * 授权用户角色
 * @returns null
 */
export const accreditUser = async (commitForm) => {
    await request.put('/auth/role/accredit', commitForm);
    ElMessage.success('授权员工成功');
}

/**
 * 验证角色名称是否重复
 * @returns Object
 */
export const validateRole = async (searchForm) => {
    const data = await request.get('/auth/role/validate', {
        params: searchForm
    });
    return data;
}